package day17_Inet.UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Receive {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket(9999);
       while (true){
           //接收数据
           byte[] bytes = new byte[1024];
           DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length);
           ds.receive(dp);
           byte[] data = dp.getData();
           String s = new String(data, 0,dp.getLength());
           System.out.println("接收的数据为"+s);
           if (s.equals("bye")){
               break;
           }
           //发送数据
           String s1 = scanner.next();
           byte[] bytes1 = s1.getBytes();
           InetAddress ip = InetAddress.getByName("192.168.3.172");
           DatagramPacket dp1 = new DatagramPacket(bytes1,0,bytes1.length,ip,8888);
           ds.send(dp1);
           if (s1.equals("bye")){
               break;
           }
       }
        ds.close();
    }
}
